package finalproject.controller;


import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import finalproject.model.items.Map;
import finalproject.model.items.PowerDrink;
import finalproject.model.items.Scouter;
import finalproject.model.items.Tablets;
import finalproject.model.player.Player;
import finalproject.model.player.PlayerModel;
import finalproject.model.player.fight.Ability;
import finalproject.model.player.pet.Pet;
import finalproject.model.rooms.Room;
import finalproject.view.GameView;
import finalproject.view.StringManager;
import finalproject.view.parser.Parser;

/**
 * This class is the main class of the "World of Zuul" application.
 * "World of Zuul" is a very simple, text based adventure game. Users can walk
 * around some scenery. That's all. It should really be extended to make it more
 * interesting!
 * 
 * To play this game, create an instance of this class and call the "play"
 * method.
 * 
 * This main class creates and initialises all the others: it creates all rooms,
 * creates the parser and starts the game. It also evaluates and executes the
 * commands that the parser returns.
 * 
 * @author Michael Kölling and David J. Barnes
 * @version 2011.08.11
 */

public class Game implements ActionListener{

	/** Command parser */
	private Parser parser;

	/** Player model */
	private PlayerModel playerModel = PlayerModel.getInstance();

	/** Game View */
	private GameView gameView;

	/**
	 * Create the game and initialise its internal map.
	 */
	public Game(Parser parser, Room startingRoom, GameView gameview, String... nicknames)
	{
		this.parser = parser;
		
		this.gameView = gameview;
		this.gameView.addTextFieldActionListener(this);
		
		ArrayList<Ability> abilities = new ArrayList<Ability>();
		abilities.add(new Ability("eclaire", 3));
		abilities.add(new Ability("vive-attaque", 2));
		abilities.add(new Ability("charge", 1));
		abilities.add(new Ability("mimiqueue", 40));
		
		// Create players
		for (String nickname : nicknames)
			playerModel.addPlayer(new Player(nickname, startingRoom, new Pet("Pikachu", abilities)));
	}

	/**
	 * Main play routine. Loops until end of play.
	 */
	public void play()
	{
		// TODO === remove (for tests)
		playerModel.getPlayer("Henri").getItemHolder().addItem(new Tablets());
		playerModel.getPlayer("Henri").getItemHolder().addItem(new Map());
		playerModel.getPlayer("Henri").getItemHolder().addItem(new Scouter());
		playerModel.getPlayer("Henri").getItemHolder().addItem(new PowerDrink());
		// ===

		// Display welcome message
		printWelcome();
		StringManager.getInstance().sendToDisplayer(Color.ORANGE, playerModel.getCurrentPlayer().getNickname() + "> ");
	}

	/**
	 * Print out the opening message for the player.
	 */
	public void printWelcome()
	{
		StringManager.outPut("welcome");
		StringManager.outPut("zuuldescription");
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// Process command until all quit
		String command = gameView.getAndRestTextField();
		
		// Empty command
		if(command == null || command.length() <= 0)
			return;
		
		// Change player
		Player player = playerModel.getCurrentPlayer();
		if(parser.getCommand(player, command).process())
			player = playerModel.nextPlayer();

		// Game over
		if(playerModel.allPlayerFinished())
			System.exit(0);
		
		// Display >
		StringManager.getInstance().sendToDisplayer(Color.ORANGE, player.getNickname() + "> ");
	}
}
